package cn.ratel.db;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

class Column {
	String name;
	String typeName;
	int dataType;
	int size;
	Class<?> clazz;
	boolean isNullable;

	Column(String name, String typeName, int dataType, int size, boolean isNullable) {
		this.name = name;
		this.typeName = typeName;
		this.dataType = dataType;
		this.size = size;
		this.clazz = typeMap.get(dataType);
		this.isNullable = isNullable;
		if (!isNullable) {
			switch (this.clazz.getSimpleName()) {
				case "Integer":
					this.clazz = int.class;
					break;
				case "Long":
					this.clazz = long.class;
					break;
				case "Float":
					this.clazz = float.class;
					break;
				case "Double":
					this.clazz = double.class;
					break;
			}
		}
		if (clazz == null) {
			throw new RuntimeException("unknown jdbc column type : " + typeName);
		}

	}

	private static Map<Integer, Class<?>> typeMap = new HashMap<Integer, Class<?>>() {
		{
			put(Types.CHAR, String.class);
			put(Types.VARCHAR, String.class);
			put(Types.LONGVARCHAR, String.class);
			put(Types.NUMERIC, java.math.BigDecimal.class);
			put(Types.DECIMAL, java.math.BigDecimal.class);
			put(Types.BIT, Boolean.class);
			put(Types.TINYINT, Integer.class);
			put(Types.SMALLINT, Integer.class);
			put(Types.INTEGER, Integer.class);
			put(Types.BIGINT, Long.class);
			put(Types.REAL, Float.class);
			put(Types.FLOAT, Double.class);
			put(Types.DOUBLE, Double.class);
			put(Types.BINARY, byte[].class);
			put(Types.VARBINARY, byte[].class);
			put(Types.LONGVARBINARY, byte[].class);
			put(Types.DATE, Long.class);
			put(Types.TIME, Long.class);
			put(Types.TIMESTAMP, Long.class);
		}
	};
}
